import os
import shutil
from xml.dom.minidom import parseString
from xml.etree.ElementTree import parse

import dicttoxml

from CommonOperations.get_all_configs import get_local_config
from CommonOperations.parse_img import create_img
from CommonOperations.parse_zip import create_zip_file
from CommonOperations.project_root_path import project_root_path


def create_xml_demo_file():
    """
    创建xml模板文件
    :return: 模板文件路径
    """
    xml_demo = get_local_config()
    img_list = []
    img_list.append({"name": "图片1", "width": 100, "height": 100})
    img_list.append({"name": "图片2", "width": 200, "height": 100})
    xml_demo['Dir_setting'] = {"dir_name": "批量文件夹xml", "whether_zip": False}
    xml_demo['Image_list'] = img_list

    xml_demo_string = dicttoxml.dicttoxml(xml_demo, custom_root="batchImages").decode('utf-8')
    pretty_xml = parseString(xml_demo_string).toprettyxml(indent='    ')

    xml_demo_path = os.path.join(project_root_path(), "TemplateDemos", "Demo.xml")
    demo_dir = os.path.join(project_root_path(), "TemplateDemos")
    if not os.path.exists(demo_dir):
        os.mkdir(demo_dir)
    if os.path.exists(xml_demo_path):
        os.remove(xml_demo_path)
    with open(xml_demo_path, 'w') as j_d:
        j_d.write(pretty_xml)

    return xml_demo_path

def batch_create_imgs_from_xml(file_path):
    """
    批量生成图片
    :param file_path: xml配置文件
    :return: 批量文件夹路径
    """
    with open(file=file_path, mode='r') as f_p:
        origin_xml = parse(f_p)

        default_w = origin_xml.findtext("Image_setting/default_w")
        default_h = origin_xml.findtext("Image_setting/default_h")
        border_color = origin_xml.findtext("Image_setting/border_color")
        border_width = origin_xml.findtext("Image_setting/border_width")
        whether_add_border = origin_xml.findtext("Image_setting/whether_add_border")
        default_jpg = origin_xml.findtext("Image_setting/default_jpg")
        default_png = origin_xml.findtext("Image_setting/default_png")
        default_gif = origin_xml.findtext("Image_setting/default_gif")

        default_size = origin_xml.findtext("Font_setting/default_size")
        default_color = origin_xml.findtext("Font_setting/default_color")
        whether_add_text = origin_xml.findtext("Font_setting/whether_add_text")

        whether_timestamp = origin_xml.findtext("Time_setting/whether_timestamp")

        dir_name = origin_xml.findtext("Dir_setting/dir_name")
        whether_zip = origin_xml.findtext("Dir_setting/whether_zip")

        img_list = origin_xml.iterfind("Image_list/item")

    # 初始化批量导出文件夹
    batch_imgs_root_dir = os.path.join(project_root_path(), "BatchImages")
    if not os.path.exists(batch_imgs_root_dir):
        os.mkdir(batch_imgs_root_dir)
    batch_imgs_dir = os.path.join(batch_imgs_root_dir, dir_name)
    if not os.path.exists(batch_imgs_dir):
        os.mkdir(batch_imgs_dir)
    # 清空文件夹
    shutil.rmtree(batch_imgs_dir)

    # 批量出图片
    for img in img_list:
        img_name = img.findtext('name')
        img_width = img.findtext('width')
        img_height = img.findtext('height')
        if default_jpg.upper() == "TRUE":
            create_img(img_width=int(img_width) if int(img_width) > 0 else default_w,
                       img_height=int(img_height) if int(img_height) > 0 else default_h,
                       whether_add_text=True if whether_add_text.upper() == "TRUE" else False,
                       font_color=default_color,
                       font_size=int(default_size),
                       desc_text=img_name,
                       whether_add_timestamp=True if whether_timestamp.upper() == "TRUE" else False,
                       img_type="JPG",
                       img_dir=batch_imgs_dir,
                       whether_add_border=True if whether_add_border.upper() == "TRUE" else False,
                       border_color=border_color,
                       border_width=int(border_width))
        if default_png.upper() == "TRUE":
            create_img(img_width=int(img_width) if int(img_width) > 0 else default_w,
                       img_height=int(img_height) if int(img_height) > 0 else default_h,
                       whether_add_text=True if whether_add_text.upper() == "TRUE" else False,
                       font_color=default_color,
                       font_size=int(default_size),
                       desc_text=img_name,
                       whether_add_timestamp=True if whether_timestamp.upper() == "TRUE" else False,
                       img_type="PNG",
                       img_dir=batch_imgs_dir,
                       whether_add_border=True if whether_add_border.upper() == "TRUE" else False,
                       border_color=border_color,
                       border_width=int(border_width))
        if default_gif.upper() == "TRUE":
            create_img(img_width=int(img_width) if int(img_width) > 0 else default_w,
                       img_height=int(img_height) if int(img_height) > 0 else default_h,
                       whether_add_text=True if whether_add_text.upper() == "TRUE" else False,
                       font_color=default_color,
                       font_size=int(default_size),
                       desc_text=img_name,
                       whether_add_timestamp=True if whether_timestamp.upper() == "TRUE" else False,
                       img_type="GIF",
                       img_dir=batch_imgs_dir,
                       whether_add_border=True if whether_add_border.upper() == "TRUE" else False,
                       border_color=border_color,
                       border_width=int(border_width))

    if whether_zip.upper() == "TRUE":
        zip_file = create_zip_file(batch_imgs_dir)
        return zip_file

    return batch_imgs_dir
